﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeShare.WeiXin.Common;
using WeShare.WeiXin.Mp.Menu.Entity;

namespace WeShare.WeiXin.Mp.Menu
{
    public class MenuApi
    {
        /// <summary>
        /// 创建普通菜单
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="btns"></param>
        /// <returns></returns>
        public static BaseRes Create(List<ButtonInfo> btns, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var data = new { button = btns };
            return HttpUtil.PostResult<BaseRes>(data, url);
        }
        /// <summary>
        /// 添加个性化菜单
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="btns"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        public static MenuId AddConditional(List<ButtonInfo> btns, Matchrule rule, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var data = new { button = btns, matchrule = rule };
            return HttpUtil.PostResult<MenuId>(data, url);
        }
        /// <summary>
        /// 删除个性化菜单
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public static BaseRes DeleteConditional(string menuId, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var data = new { menuid = menuId };
            return HttpUtil.PostResult<BaseRes>(data, url);
        }
        /// <summary>
        /// 测试个性化菜单匹配结果
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static MenuInfo TryMatch(string userId, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token={OpenApi.GetToken(tokenOrAppId)}";
            var data = new { user_id = userId };
            return HttpUtil.PostResult<MenuInfo>(data, url);
        }
        /// <summary>
        /// 自定义菜单查询接口
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static MenuInfo Get(string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/get?access_token={OpenApi.GetToken(tokenOrAppId)}";
            return HttpUtil.GetResult<MenuInfo>(url);
        }
        /// <summary>
        /// 获取自定义菜单配置
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static SelfMenu GetSelfMenu(string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token={OpenApi.GetToken(tokenOrAppId)}";
            return HttpUtil.GetResult<SelfMenu>(url);
        }
#if !NET40
        /// <summary>
        /// 获取自定义菜单配置
        /// </summary>
        /// <param name="tokenOrAppId"></param>
        /// <returns></returns>
        public static async Task<SelfMenu> GetSelfMenuAsync(string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            return await HttpUtil.GetResultAsync<SelfMenu>(url);
        }
        /// <summary>
        /// 创建普通菜单
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="btns"></param>
        /// <returns></returns>
        public static async Task<BaseRes> CreateAsync(List<ButtonInfo> btns, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var data = new { button = btns };
            return await HttpUtil.PostResultAsync<BaseRes>(data, url);
        }
        /// <summary>
        /// 添加个性化菜单
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="btns"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        public static async Task<MenuId> AddConditionalAsync(List<ButtonInfo> btns, Matchrule rule, string tokenOrAppId)
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var data = new { button = btns, matchrule = rule };
            return await HttpUtil.PostResultAsync<MenuId>(data, url);
        }
        /// <summary>
        /// 删除个性化菜单
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public static async Task<BaseRes> DeleteConditionalAsync(string menuId, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var data = new { menuid = menuId };
            return await HttpUtil.PostResultAsync<BaseRes>(data, url);
        }
        /// <summary>
        /// 测试个性化菜单匹配结果
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static async Task<MenuInfo> TryMatchAsync(string userId, string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            var data = new { user_id = userId };
            return await HttpUtil.PostResultAsync<MenuInfo>(data, url);
        }
        /// <summary>
        /// 自定义菜单查询接口
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static async Task<MenuInfo> GetAsync(string tokenOrAppId = "")
        {
            var url = $"https://api.weixin.qq.com/cgi-bin/menu/get?access_token={await OpenApi.GetTokenAsync(tokenOrAppId)}";
            return await HttpUtil.GetResultAsync<MenuInfo>(url);
        }
#endif
    }
}
